using System;
using System.Collections;
using System.Collections.Generic;

using NHibernate;

using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;

namespace Adge.Models
{
    [ActiveRecord("BlogTable")]
    public class Blog : AdgeARBase<Blog>
    {
        private int _id;
        private String _name;
        private String _author;
        private IList<Post> _posts = new List<Post>();
        private IList _publishedposts;
        private IList _unpublishedposts;
        private IList _recentposts;

        [PrimaryKey]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [Property]
        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [Property]
        public String Author
        {
            get { return _author; }
            set { _author = value; }
        }

        [HasMany(Table = "Posts", ColumnKey = "blogid")]
        public IList<Post> Posts
        {
            get { return _posts; }
            set { _posts = value; }
        }

        [HasMany(typeof(Post), Table = "Posts", ColumnKey = "blogid", Where = "published = 1")]
        public IList PublishedPosts
        {
            get { return _publishedposts; }
            set { _publishedposts = value; }
        }

        [HasMany(typeof(Post), Table = "Posts", ColumnKey = "blogid", Where = "published = 0")]
        public IList UnPublishedPosts
        {
            get { return _unpublishedposts; }
            set { _unpublishedposts = value; }
        }

        [HasMany(typeof(Post), Table = "Posts", ColumnKey = "blogid", OrderBy = "created desc")]
        public IList RecentPosts
        {
            get { return _recentposts; }
            set { _recentposts = value; }
        }

        public void CustomAction()
        {
            ActiveRecordMediator<Blog>.Execute(new NHibernateDelegate(MyCustomMethod), this);
        }

        private object MyCustomMethod(ISession session, object blogInstance)
        {
            session.Delete(blogInstance);
            session.Flush();

            return null;
        }

        internal static ISessionFactoryHolder Holder
        {
            get { return ActiveRecordMediator<Blog>.GetSessionFactoryHolder(); }
        }
    }
}
